<?php
namespace app\index\controller;

use app\BaseController;
use think\facade\View;
//常规验证规则 生成器
class Validatex extends Front
{
    /**
     * 默认规则提示
     * @var array
     */
    protected $typeMsg = [
        'require'     => ':attribute require',
        'must'        => ':attribute must',
        'number'      => ':attribute must be numeric',
        'integer'     => ':attribute must be integer',
        'float'       => ':attribute must be float',
        'email'       => ':attribute not a valid email address',
        'mobile'      => ':attribute not a valid mobile'
    ];
    protected $defaultRegex = [
        'alpha'       => '/^[A-Za-z]+$/',
        'alphaNum'    => '/^[A-Za-z0-9]+$/',
        'alphaDash'   => '/^[A-Za-z0-9\-\_]+$/',
        'chs'         => '/^[\x{4e00}-\x{9fa5}]+$/u',
        'chsAlpha'    => '/^[\x{4e00}-\x{9fa5}a-zA-Z]+$/u',
        'chsAlphaNum' => '/^[\x{4e00}-\x{9fa5}a-zA-Z0-9]+$/u',
        'chsDash'     => '/^[\x{4e00}-\x{9fa5}a-zA-Z0-9\_\-]+$/u',
        'mobile'      => '/^1[3-9]\d{9}$/',
        'idCard'      => '/(^[1-9]\d{5}(18|19|([23]\d))\d{2}((0[1-9])|(10|11|12))(([0-2][1-9])|10|20|30|31)\d{3}[0-9Xx]$)|(^[1-9]\d{5}\d{2}((0[1-9])|(10|11|12))(([0-2][1-9])|10|20|30|31)\d{3}$)/',
        'zip'         => '/\d{6}/',
    ];
    public function index()
    {
        $validlist = $this->validlist();
        View::assign('validlist',$validlist);
        return View::fetch('index');
    }
    public function post()
    {
    	//eg:主要应用于复杂度较低的验证规则的辅助生成
    	$rule_list = [
    		'num'=>[
    			//验证规则 支持多个验证规则
    			'numsection'=>[
    				'max'=>500,
    				'min'=>0
    			]
    		]
    	];
    	//var_dump($this->data);
    	//数据处理
    	$data = [];
    	foreach ($this->data as $k => $v) {
    		list($i,$field) = explode('_', $k);
    		if(!isset($data[$i])){
    			$data[$i] = [];
    		}
    		$data[$i][$field] = $v;
    	}
    	var_dump($data);
    	die;
    }
    /**
     * [validlist 可用验证方式列表]
     * @return [type] [description]
     */
    public function validlist()
    {
    	return [
    		'numsection'=>[
    			'name'=>'数值区间',
                'k'=>'numsection',
    			'params'=>[
    				'max'=>[
                        'name'=>'最大值',
                        'tmp'=>'<input name="{{i}}_min" class="min" placeholder="最小值" />'
                    ],
    				'min'=>[
                        'name'=>'最大值',
                        'tmp'=>'<input name="{{i}}_max" class="max" placeholder="最大值" />'
                    ]
    			]
    		],
    		'email'=>[
    			'name'=>'邮箱',
                'k'=>'email',
    			'params'=>[]
    		],
            'phone'=>[
                'name'=>'电话',
                'k'=>'phone',
                'params'=>[]
            ],
            'pwd'=>[
                'name'=>'密码',
                'k'=>'pwd',
                'params'=>[
                    'minlen'=>[
                        'name'=>'最小长度',
                        'tmp'=>'<input name="{{i}}_minlen" class="minlen" placeholder="最小长度" />'
                    ],
                    'maxlen'=>[
                        'name'=>'最大长度',
                        'tmp'=>'<input name="{{i}}_maxlen" class="maxlen" placeholder="最大长度" />'
                    ]
                ]
            ],
            'date'=>[
                'name'=>'日期',
                'k'=>'date',
                'params'=>[]
            ],
            'alpha'=>[
                'name'=>'字母',
                'k'=>'alpha',
                'params'=>[]
            ],
            'chs'=>[
                'name'=>'汉字',
                'k'=>'chs',
                'params'=>[]
            ]
    	];
    }
    /**
     * [parseValide 验证规则解析]
     * @return [type] [description]
     */
    public function  parseValide()
    {

    }
    /**
     * [packageValideRule 组装生成验证规则]
     * @return [type] [description]
     */
    public function packageValideRule($data)
    {
        
    }
}
